<template>
  <!-- 添加或修改收货人对话框 -->
  <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-tabs v-model="activeName" class="detail-tab" type="card">
        <el-tab-pane label="基本信息" name="first">
          <el-row :gutter="24">
            <!--<el-col :span="12">
                            <el-form-item label="收货人编码" prop="code">
                                <el-input v-model="form.code" placeholder="请输入收货人编码"/>
                            </el-form-item>
                        </el-col>-->
            <el-col :span="12">
              <el-form-item label="收货人姓名" prop="shouhuoren">
                <el-input v-model="form.shouhuoren" placeholder="请输入收货人名称" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="收货人电话" prop="shouhuotel">
                <el-input v-model="form.shouhuotel" placeholder="请输入收货人电话" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="12">
              <el-form-item label="业务经理及电话" prop="jingli">
                <el-input v-model="form.jingli" placeholder="请输入业务经理及电话" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户" prop="customId">
                <el-select v-model="form.customId" placeholder="请选择客户" filterable @change="selectCustomer" clearable style="width: 100%;">
                  <el-option v-for="dict in dict.type.goods_customer" ref="myselectCustomer" :key="dict.value"
                    :label="dict.label" :value="dict.value" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="24">
            <el-col :span="24">
              <el-form-item label="收货地址" prop="address">
                <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
              </el-form-item>
            </el-col>
            <!-- <el-col :span="12">
                            <el-form-item label="客户名称" prop="custonName">
                                <el-input v-model="form.custonName" placeholder="请输入客户名称"/>
                            </el-form-item>
                        </el-col>-->

            <el-col :span="24">
              <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" placeholder="请输入备注" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    addAction,
    getById,
    updateAction
  } from '@/api/manage';
  import moment from 'moment';

  export default {
    name: "GoodsShouhuorenForm",
    dicts: ['goods_customer'],
    data() {
      return {
        // 遮罩层
        loading: true,
        activeName: "first",
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          shouhuoren: [{
            required: true,
            message: "收货人不能为空",
            trigger: "blur"
          }],
          shouhuotel: [{
            required: true,
            message: "收货人电话不能为空",
            trigger: "blur"
          }]
        },
        url: {
          getById: '/goodsShouhuoren/goodsShouhuoren/',
          add: '/goodsShouhuoren/goodsShouhuoren/add',
          edit: '/goodsShouhuoren/goodsShouhuoren/edit',
        },
      };
    },
    created() {

    },
    methods: {
      selectCustomer(value) {
        let that = this;
        that.$nextTick(() => {
          let arr = that.$refs.myselectCustomer;
          for (let i = 0; i < arr.length; i++) {
            if (arr[i].currentValue == value) {
              that.form.customName = arr[i].label;
            }
          }
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          delFlag: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          code: null,
          shouhuoren: null,
          shouhuotel: null,
          address: null,
          customId: null,
          custonName: null
        };
        this.resetForm("form");
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加收货人";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getById(this.url.getById, id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改收货人";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateAction(this.url.edit, this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.$emit('ok');
              });
            } else {
              addAction(this.url.add, this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.$emit('ok');
              });
            }
          }
        });
      },
    }
  };
</script>
